package com.jss.lanouhn.fristapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.modles.HomeItemInfos;
import com.squareup.picasso.Picasso;

import java.io.IOException;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/9/1.
 */
public class HomeItemDetail extends AppCompatActivity {
    private String urlHead = "http://api.liwushuo.com/v2/posts_v2/";
    private String url;
    private HomeItemInfos iteminfos;
    private ImageView iv_Title, iv_back, iv_share;
    private WebView wb;
    private TextView tv1_detail, tv2_detail, tv3_detail, tv_all;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_detail);
        initData();
        initForWebData();
    }


    private void initData() {
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        url = urlHead.trim() + id.trim();


        wb = (WebView) findViewById(R.id.wv_detailWebView);
        iv_Title = (ImageView) findViewById(R.id.iv_home_details);
        iv_back = (ImageView) findViewById(R.id.iv_detailWebView);
        iv_share = (ImageView) findViewById(R.id.iv_home_detail);
        iv_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showShare();
            }
        });
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_all = (TextView) findViewById(R.id.tv_act_detail);
        tv1_detail = (TextView) findViewById(R.id.tv1_detailWeb_view);
        tv2_detail = (TextView) findViewById(R.id.tv2_detailWeb_view);
        tv3_detail = (TextView) findViewById(R.id.tv3_detailWeb_view);
    }

    private void initForWebData() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Gson gson = new Gson();
                iteminfos = gson.fromJson(result, HomeItemInfos.class);
                handler.sendEmptyMessage(1);


            }
        });

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    HomeItemInfos.DataBean bean = iteminfos.getData();
                    Picasso.with(HomeItemDetail.this).load(bean.getCover_image_url())
                            .into(iv_Title);
                    tv1_detail.setText(bean.getLikes_count() + "");
                    tv2_detail.setText(bean.getShares_count() + "");
                    tv3_detail.setText(bean.getComments_count() + "");

                    LoadWeb(iteminfos);

                    break;
            }
        }
    };

    private void LoadWeb(final HomeItemInfos iteninfos) {

        wb.loadDataWithBaseURL(null, iteninfos.getData().getContent_html(), "text/html", "utf-8", null);
        wb.getSettings().setJavaScriptEnabled(true);


//        WebSettings webSettings = wb.getSettings();
//
//        webSettings.setJavaScriptEnabled(true);
        wb.reload();
        wb.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadDataWithBaseURL(null, iteninfos.getData().getContent_html(), "text/html", "utf-8", null);
                return true;
            }
        });
    }

    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
//        oks.setTitle(getString(R.string.share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
        oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(this);
    }

}
